package tech.lithum.mm.api;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import tech.lithum.mm.adapter.Topic;

public class Api {
    private String username = "";
    private String password = "";
    private String baseUrl = "";
    private String token = "";
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static Api api = null;

    public Api(String username, String password, String baseUrl) {
        this.username = username;
        this.password = password;
        this.baseUrl = baseUrl;
    }

    public static Api getApi() {
        if(api == null) {
//            api = new Api("admin", "27a3f7b3406acd7eebecc2843a9c4edb76d5108676b2b4af296a63bb46d64b1ebbc3e69269c8169dbb0fc40a35747626503bffdba94245e5462d9461cda08dec", "http://139.224.62.22:7777/api");
            api = new Api("admin", "e4aed93ec8e0084edaef0cb945aa5acb885792dea7c115f6de9a96c77df0ca617761738c76e965f18aafc30eccbe0dacc9ec1788a7a1bbc0d6ef59b98047c099", "http://192.168.1.7:8081/api");
        }
        return api;
    }

    public LoginResponse login() {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setUsername(this.username);
        loginRequest.setPassword(this.password);
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(new Gson().toJson(loginRequest), JSON);
        Request request = new Request.Builder().url(baseUrl + "/login").post(requestBody).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            LoginResponse loginResponse = new Gson().fromJson(response.body().string(), LoginResponse.class);
            return loginResponse;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Topic> getTopics() {
        if(token.isEmpty()) {
            LoginResponse response = login();
            token = response.getToken();
        }

        RunWorkflowRequest runWorkflowRequest = new RunWorkflowRequest();
        runWorkflowRequest.setSync(true);
        runWorkflowRequest.setName("微博");
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(new Gson().toJson(runWorkflowRequest), JSON);
        Request request = new Request.Builder().url(baseUrl + "/run").header("Authorization", "Bearer " + token).post(requestBody).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            RunWorkflowResponse runWorkflowResponse = new Gson().fromJson(response.body().string(), RunWorkflowResponse.class);
            TopicResponse obj1 = new Gson().fromJson(runWorkflowResponse.getData(), TopicResponse.class);
            return obj1.getGet_weibo_data();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
